package cn.stylefeng.guns.modular.gridsystem.hechuang;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.gridsystem.utils.MyFileUtils;
import lombok.extern.slf4j.Slf4j;
import net.aivoicetech.wav.WavFeatureDetect;
import net.aivoicetech.wav.entity.WavFeature;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 图形接口
 */


@RestController
@Slf4j
//@RequiredArgsConstructor
public class GraphController {

    /**
     * @param file
     * @return
     * @调用的方法
     */
    public static List<Map<String, Object>> findGraphData(File file, WavFeatureDetect wavFeatureDetect) {
        List<Map<String, Object>> arrayList = new ArrayList<>();
        try {
            //获取定义图谱类型的集合
            List<WavFeature> wavFeatureList = wavFeatureDetect.getFeature(file);

            for (WavFeature wavFeature : wavFeatureList) {
                ArrayList data = (ArrayList) wavFeature.getData();
                final float stepTimeLength = wavFeature.getStepTimeLength();
                double[] subscript = new double[data.size()];
                if (wavFeature.getType().equals(WavFeatureDetect.FeatureType.SoundPressLevel.name())) {
                    for (int j = 0; j < data.size(); j++) {
                        subscript[j] = Math.floor(((j * stepTimeLength) / 1000) * 10) / 10;
                        data.set(j, Double.parseDouble(String.valueOf(data.get(j))) + 100);
                    }
                } else if (wavFeature.getType().equals(WavFeatureDetect.FeatureType.Spectrogram.name())) {
                    int[] data0 = (int[]) data.get(0);
                    subscript = new double[data0.length];
                    for (int j = 0; j < data0.length; j++) {
                        subscript[j] = wavFeature.getSampleRate() / 2 / data0.length * (j + 1);
                        data.set(j, data0[j]);
                    }
                } else {
                    for (int j = 0; j < data.size(); j++) {
                        subscript[j] = Math.floor(((j * stepTimeLength) / 1000) * 10) / 10;
                    }
                }
                HashMap<String, Object> map = new HashMap<>();
                map.put("xData", subscript);
                map.put("yData", data);
                map.put("type", wavFeature.getType());
                arrayList.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取设备信息图谱异常>>> {}", "getGraphData()");
        }
        return arrayList;
    }


    /**
     * @param mFile
     * @return
     * @获取设备信息图谱 <p>
     * SoundWav 波形图
     * FREQUENCY 时频图 （故障对比）
     * SoundPressLevel 声压级
     * Spectrogram 时频域图 （语谱图）
     */
    @PostMapping("/graph/getGraphList")
    public ResponseData getGraphByMfile(@RequestParam(value = "mFile") MultipartFile mFile) {
        if (ObjectUtil.isEmpty(mFile)) {
            return ResponseData.error("参数异常，请查验！");
        }
        WavFeatureDetect wavFeatureDetect = WavFeatureDetect.build(WavFeatureDetect.FeatureType.SoundWav, WavFeatureDetect.FeatureType.FREQUENCY,
                WavFeatureDetect.FeatureType.SoundPressLevel, WavFeatureDetect.FeatureType.Spectrogram);
        //文件转换
        String path = MyFileUtils.getUploadPath() + File.separator + mFile.getOriginalFilename();
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        try {
            //multipartFile 写入到本地磁盘指定的路径下
            mFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> list = findGraphData(file, wavFeatureDetect);
        file.delete();
        return ResponseData.success(list);
    }

    /**
     * @param mFile
     * @return
     * @获取设备信息图谱 SoundWav 波形图
     * FREQUENCY 时频图 （故障对比）
     * SoundPressLevel 声压级
     * Spectrogram 时频域图 （语谱图）
     */
    @PostMapping("/graph/getGraphData")
    public ResponseData getGraphByParam(MultipartFile mFile, String type) {
        // public ResponseData getGraphByParam(@Valid @RequestParam(value = "mFile") MultipartFile mFile, @RequestParam(value = "type")  @NotEmpty(message = "参数异常，请查验！") String type ) { 只是服务器后台提示错误
        if (ObjectUtil.isEmpty(mFile) || StrUtil.isBlank(type)) {
            return ResponseData.error("参数异常，请查验！");
        }
        WavFeatureDetect.FeatureType featureType = null;
        if (type.equals("SoundWav")) {
            featureType = WavFeatureDetect.FeatureType.SoundWav;
        } else if (type.equals("FREQUENCY")) {
            featureType = WavFeatureDetect.FeatureType.FREQUENCY;
        } else if (type.equals("SoundPressLevel")) {
            featureType = WavFeatureDetect.FeatureType.SoundPressLevel;
        } else if (type.equals("Spectrogram")) {
            featureType = WavFeatureDetect.FeatureType.Spectrogram;
        }
        WavFeatureDetect wavFeatureDetect = WavFeatureDetect.build(featureType);
        //文件转换
        String path = MyFileUtils.getUploadPath() + File.separator + mFile.getOriginalFilename();
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        try {
            //multipartFile 写入到本地磁盘指定的路径下
            mFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> list = findGraphData(file, wavFeatureDetect);
        file.delete();
        return ResponseData.success(list.get(0));
    }


}
